//
//  LFTMCUnReadAlertView.m
//  Pods
//
//  Created by Thomas Quan on 2017/5/9.
//
//

#import "LFTMCUnReadAlertView.h"
#import "NTESGlobalMacro.h"

@interface LFTMCUnReadAlertView()

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, assign) NSUInteger count;

@end

@implementation LFTMCUnReadAlertView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.count = 0;
        CGFloat screenWidth  = [UIScreen mainScreen].bounds.size.width;
        CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
        
        CGFloat timerWidth = 145;
        CGFloat timerHeight = 35;
        
        CGFloat x = screenWidth / 2 - timerWidth / 2;
        CGFloat y = screenHeight / 2 - timerHeight / 2;
        
        self.bounds = CGRectMake(0, 0, timerWidth, timerHeight);
        
        [self setBackgroundColor:[UIColor whiteColor]];
        
        self.layer.cornerRadius = 15;
        self.layer.shadowOpacity = 0.8f;
        self.layer.shadowColor = [UIColor lightGrayColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(2,2);
        self.layer.shadowRadius = 2;
        
        UIImage *image = [UIImage imageNamed:@"LFTMC_Triangle"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        [self addSubview:imageView];
        
        imageView.center = CGPointMake(timerWidth/5, timerHeight/2);
        imageView.bounds = CGRectMake(0, 0, 5, 7);
        
        _label = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:_label];

        _label.text = [NSString stringWithFormat:@"%ld条新消息", self.count];
        _label.textColor = UIColorFromRGB(0x97CC00);
        _label.font = [UIFont systemFontOfSize:14];
        
        [_label sizeToFit];
        
        _label.center = CGPointMake(timerWidth*3/5, timerHeight/2);
        _label.bounds = CGRectMake(0, 0, 83, 14);
    }
    return self;
}

- (void)resetSelf {
    self.count = 0;
    [self setHidden:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if ([self.delegate respondsToSelector:@selector(didClickLFTMCUnreadAlertView)]) {
        [self.delegate didClickLFTMCUnreadAlertView];
    }
}

- (void)updateCount:(int)count {
    if (count == 0) {
        return;
    }
    
    if ([self isHidden]) {
        [self setHidden:NO];
        self.count = count;
    } else {
        self.count += count;
    }
    
    self.label.text = [NSString stringWithFormat:@"%ld条新消息", self.count];
}

@end
